package com.example.shengyin.config;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author oldliu
 * @since 1.0
 */
@Service
public class TimeInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        System.out.println("url:"+request.getRequestURL());
//        if ((request.getSession().getAttribute("user")) == null) {
//           // System.out.println("request.getSession().getAttribute(\"user\")"+request.getSession().getAttribute("user").toString());
//            response.getWriter().print("{\"code\":401,\"msg\":\"您还没有登录，不能访问\"}");
//            return false;
//        }
        return true;
    }

    @Override//handler执行完成后，执行此方法
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
//        System.out.println("handler:" + handler + ",返回结果是 " + modelAndView);
//
//        long start = (Long) request.getAttribute("start");
//        long end = System.nanoTime();
//        long time = end - start;
//        System.out.println("请求：" + request.getServletPath() + "耗时：" + time + "纳秒");

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                                Exception ex) throws Exception {
    }
}
